#!/bin/bash
# ubuntu 使用阿里源安装 docker


#check
[[ $UID -ne 0 ]] && { echo "Must run in root user !";exit; }
[[ -f /etc/debian_version ]] || { echo "It's only used for Ubuntu or Debian";exit; }

#替换阿里源#
#backup
sudo cp /etc/apt/sources.list{,.bak}
sed -i 's/archive.ubuntu.com/mirrors.aliyun.com/' /etc/apt/sources.list

#docker
# step 1: 安装必要的一些系统工具
sudo apt-get update
sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common
# step 2: 安装GPG证书
curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
# Step 3: 写入软件源信息
sudo add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
sudo echo 'deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu artful stable' >>/etc/apt/sources.list
# Step 4: 更新并安装 Docker-CE
sudo apt-get -y update
sudo apt-get -y install docker-ce

#阿里云加速(可自己申请地址)#
sed -i 's/--registry-mirro.*aliyuncs.com//' /lib/systemd/system/docker.service

mkdir -p /etc/docker/
cat>/etc/docker/daemon.json<<EOF
{
  "exec-opts": ["native.cgroupdriver=systemd"],
  "registry-mirrors": ["https://y3asdhja.mirror.aliyuncs.com"],
  "storage-driver": "overlay2",
  "storage-opts": ["overlay2.override_kernel_check=true"],
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "100m",
    "max-file": "3"
  }
}
EOF

echo 'start docker'
systemctl daemon-reload
systemctl enable  docker
systemctl restart  docker

